<?php

class Model_listing extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->helper('text');
    }

    function get_listing() {        
        $sql = "SELECT properti.properti_id, properti.nama, properti.harga, alamat.kota_kab, user.nama_lengkap, user.user_id
                from properti,alamat,user
                where properti.alamat_id = alamat.alamat_id and properti.user_id = user.user_id";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $value) {
                $a[] = $value;
            }
            return $a;
        } else {
            return FALSE;
        }
    }

    function get_properti($where = array()) {
        $this->db->select('properti_id,nama_properti,tipe,luas_bangunan,luas_tanah,jml_kmr_tidur,jml_kmr_mandi,harga,sewa_beli');
        return $this->db->get_where('properti', $where);
    }

    function get_listing_admin($level="",$id=""){
        if($level == 1){
            $sql = "select properti_id,nama_properti,harga,tipe,luas_bangunan,jml_kmr_tidur,jml_kmr_mandi,sewa_beli
                from properti";//perlu ditambahin alamat
            $query = $this->db->query($sql);
        } else {
            $sql = "select user_id,properti_id,nama_properti,harga,tipe,luas_bangunan,jml_kmr_tidur,jml_kmr_mandi,sewa_beli
                from properti where user_id=?";//perlu ditambahin alamat
            $query = $this->db->query($sql,$id);
        }
        
        if ($query->num_rows() > 0) {
            return $query;
        } else {
            return FALSE;
        }
    }

    function get_fasilitas_properti($str_id=''){
        if($str_id == ''){
            return FALSE;
        }
        $sql = "select * from properti,fasilitas where fasilitas.fasilitas_id = properti.fasilitas_id
                and properti.properti_id = ?";
        $query = $this->db->query($sql,$str_id);
        if ($query->num_rows() > 0) {
            return $query;
        } else {
            return FALSE;
        }
    }

    function get_listing_detail($str_id=''){
        if($str_id == ''){
            return FALSE;
        }
        $sql = "SELECT *
                from properti where properti_id = ?";
        $query = $this->db->query($sql,$str_id);
        if($query->num_rows() == 1){
            $value = $query->row();
            return $value;
        } else { echo 'lalala';
            return FALSE;
        }
    }

    function get_agen_detail_properti($str_id=''){
        if($str_id == ''){
            return FALSE;
        }
        $sql = "SELECT *
                from user,perusahaan,alamat,properti
                where user.alamat_id = alamat.alamat_id and user.perusahaan_id = perusahaan.perusahaan_id
                and user.user_id = properti.user_id";
        $query = $this->db->query($sql,$str_id);
        if($query->num_rows() == 1){
            $value = $query->row();
            return $value;
        } else {
            return FALSE;
        }
    }

    function get_jumlah_properti_agen($str_id=''){
        if($str_id == ''){
            return FALSE;
        }
        $sql = "SELECT *
                from user,properti
                where user.user_id = properti.user_id";
        $query = $this->db->query($sql,$str_id);
        $value = $query->row();

        $sql = "SELECT *
                from user,properti
                where user.user_id = properti.user_id";
        $query = $this->db->query($sql,$value->user_id);
        return $query->num_rows();
    }

    function hapus_listing($where=array()){
        $this->db->trans_begin();
        $this->db->delete('properti',$where);
        if($this->db->trans_status()==FALSE){
            $this->db->trans_rollback();
            return FALSE;
        } else {
            $this->db->trans_commit();
            return TRUE;
        }
    }

}

?>